package com.facexue.miniprogram.api;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.facexue.miniprogram.model.SmsModel;
import com.facexue.miniprogram.model.UserLoginModel;
import com.facexue.miniprogram.utils.AjaxResult;
import com.facexue.miniprogram.utils.JwtUtil;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@CrossOrigin
@RestController
@Tag(name="用户登录api",description = "里面集成了用户,员工,管理员所有人的登录")
@RequestMapping("/api/user/login")
public class UserLoginApi {
	
	@Operation(summary = "账号密码登录")
	@PostMapping("/pwdlogin")
	public AjaxResult pwdLogin(@RequestParam("username") String username,@RequestParam("password") String password,@RequestParam("usertype") Integer userType) {
		UserLoginModel uLoginModel = new UserLoginModel();
		uLoginModel.setUname(username);
		uLoginModel.setUpwd(password);
		uLoginModel.setLoginType(0);
		uLoginModel.setUserType(userType);
		try {
			Map<String,String> tokenload = new HashMap();
			tokenload.put("uid", uLoginModel.getUid().toString());
			tokenload.put("uname", uLoginModel.getUname());
			String token = JwtUtil.createToken(tokenload);
			Map<String,Object> res = new HashMap();
			res.put("token", token);
			return AjaxResult.success("认证成功", res);
		} catch (Exception e) {
			return AjaxResult.error("网络错误,连接服务器失败");
	    }
	}
	@Autowired
	SmsModel smsModel ;
	@Operation(summary = "获取验证码接口")
	@PostMapping("/verifity-code")
	public AjaxResult VerificatyCode(@RequestParam("phone")String phone) {
		
		String accesskey = smsModel.getAccesskey();
		String secret = smsModel.getSecret();
		String sign = smsModel.getSignId();
		String templateId = "284771";
		String content = URLEncoder.encode("125841##10");
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
 
        // 创建HttpEntity，包装请求头信息
        HttpEntity<String> entity = new HttpEntity<>(headers);
 
        // 设置请求的URL
        String url = "http://api.1cloudsp.com/api/v2/single_send?accesskey="+accesskey+"&secret="+secret+"&sign="+sign+"&templateId="+templateId+"&mobile="+phone+"&content="+content;
        System.out.println(url);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        return AjaxResult.success(response.getBody());
	}
	
	@Operation(summary = "手机号登录")
	@PostMapping("/phonelogin")
	public AjaxResult phoneLogin(@RequestParam("phone") String phone,@RequestParam("vcode") String vcode,@RequestParam("usertype") Integer userType) {
		UserLoginModel uLoginModel = new UserLoginModel();
		uLoginModel.setPhone(phone);
		uLoginModel.setVcode(vcode);
		uLoginModel.setLoginType(1);
		uLoginModel.setUserType(userType);
		try {
			Map<String,String> tokenload = new HashMap();
			tokenload.put("uid", uLoginModel.getUid().toString());
			tokenload.put("uname", uLoginModel.getUname());
			String token = JwtUtil.createToken(tokenload);
			Map<String,Object> res = new HashMap();
			res.put("token", token);
			return AjaxResult.success("认证成功", res);
		} catch (Exception e) {
			return AjaxResult.error("网络错误,连接服务器失败");
	    }
	}
	@Operation(summary = "邮箱登录")
	@PostMapping("/emaillogin")
	public AjaxResult emailLogin(@RequestParam("email") String email,@RequestParam("vcode") String vcode,@RequestParam("usertype") Integer userType) {
		UserLoginModel uLoginModel = new UserLoginModel();
		uLoginModel.setEmail(email);
		uLoginModel.setVcode(vcode);
		uLoginModel.setLoginType(2);
		uLoginModel.setUserType(userType);
		try {
			Map<String,String> tokenload = new HashMap();
			tokenload.put("uid", uLoginModel.getUid().toString());
			tokenload.put("uname", uLoginModel.getUname());
			String token = JwtUtil.createToken(tokenload);
			Map<String,Object> res = new HashMap();
			res.put("token", token);
			return AjaxResult.success("认证成功", res);
		} catch (Exception e) {
			return AjaxResult.error("网络错误,连接服务器失败");
	    }
	}
	@Operation(summary = "第三方登录")
	@PostMapping("/authlogin")
	public AjaxResult authLogin(@RequestParam("code") String code,@RequestParam("usertype") Integer userType) {
		UserLoginModel uLoginModel = new UserLoginModel();
		uLoginModel.setOpenid(code);
		uLoginModel.setLoginType(4);
		uLoginModel.setUserType(userType);
		try {
			Map<String,String> tokenload = new HashMap();
			tokenload.put("uid", uLoginModel.getUid().toString());
			tokenload.put("uname", uLoginModel.getUname());
			String token = JwtUtil.createToken(tokenload);
			Map<String,Object> res = new HashMap();
			res.put("token", token);
			return AjaxResult.success("认证成功", res);
		} catch (Exception e) {
			return AjaxResult.error("网络错误,连接服务器失败");
	    }
	}
}
